
//导入依耐
const gulp = require('gulp');
//API服务器---------------------------------------------------
const path = require('path');
const nodemon = require('gulp-nodemon');
const browserSync = require('browser-sync').create();
const server = path.resolve(__dirname, './api');
gulp.task('browser-sync', ['nodemon'], () => browserSync.init(null, {}));
// browser-sync 监听文件
gulp.task('api', ['browser-sync'], () => gulp.watch('./api/**/*.js', ['bs-delay']));
// 延时刷新
gulp.task('bs-delay', () => setTimeout(() => browserSync.reload(), 1000));
// 服务器重启
gulp.task('nodemon', cb => {
    // 设个变量来防止重复重启
    let started = false;
    let stream = nodemon({
        script: './api/server.js',
        ext: 'js',
        env: { NODE_ENV: 'development' },
        watch: [server]
    });
    stream
        .on('start', () => (started = !started && !cb()))
        .on('crash', () => stream.emit('restart', 10));
});